package com.example.demo.controller;

import com.example.demo.entity.Video;
import com.example.demo.entity.VideoComm;
import com.example.demo.entity.VideoLike;
import com.example.demo.repository.dao.IVideoService;
import com.example.demo.util.Common;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/video")
public class VideoAction {
    private static Logger LOGGER = LoggerFactory.getLogger(VideoAction.class);
    public static String URL = null;
    static {
        URL = "http://"+Common.getLocalIP()+":8080";
        LOGGER.info("本地IP:"+URL);
    }

    @Autowired
    IVideoService vp;

    @RequestMapping("/findAll")
    public String findAll(String paramstr){
        LOGGER.info("findall:"+paramstr);
        Map result = new HashMap();
        try{
            List<Video> videoList = vp.getAll();
            if(Common.isListNotBlank(videoList)){
                for(int i=0;i<videoList.size();i++){
                    Video v =  videoList.get(i);
                    v.setVideoUrl(URL+v.getVideoUrl());
                    v.setImgUrl(URL+v.getImgUrl());
                }
            }
            result.put("success",true);
            result.put("data", JSONArray.fromObject(videoList).toString());
        }catch (Exception e){
            result.put("success",false);
            e.printStackTrace();
        }
        return JSONObject.fromObject(result).toString();
    }

    @RequestMapping("/getVideo")
    public String getVideo(String id){
        LOGGER.info("getVideo:"+id);
        Map result = new HashMap();
        try{
            int videoId = Common.saveToInteger(id,0);
            Video video = vp.getVideo(videoId);
            video.setVideoUrl(URL+video.getVideoUrl());

            String commsStr = "";
            List<VideoComm> comms = vp.getAllCommByVideoId(videoId);
            if(Common.isListNotBlank(comms)){
                commsStr = JSONArray.fromObject(comms).toString();
            }

            result.put("success",true);
            result.put("video",JSONObject.fromObject(video).toString());
            result.put("comms",commsStr);

        }catch (Exception e){
            result.put("success",false);
            e.printStackTrace();
        }
        return JSONObject.fromObject(result).toString();
    }

    @RequestMapping("/addVideoComm")
    public String addVideoComm(String param){
        LOGGER.info("addVideoComm:"+param);
        Map result = new HashMap();
        try {
            if(Common.isEmpty(param)){
                result.put("success",false);
                result.put("msg","参数不足");
                return JSONObject.fromObject(result).toString();
            }
            JSONObject json = JSONObject.fromObject(param);
            int videoID = Common.saveToInteger(json.getString("videoID"),0);
            String nickName = Common.saveToString(json.getString("nickName"),"");
            String avatarUrl = Common.saveToString(json.getString("avatarUrl"),"");
            String gender = Common.saveToString(json.getString("gender"),"");
            String city = Common.saveToString(json.getString("city"),"");
            String province = Common.saveToString(json.getString("province"),"");
            String comm = Common.saveToString(json.getString("comm"),"");

            VideoComm vc = new VideoComm();
            vc.setVideoID(videoID);
            vc.setNickName(nickName);
            vc.setAvatarUrl(avatarUrl);
            vc.setGender(gender);
            vc.setCity(city);
            vc.setProvince(province);
            vc.setComm(comm);

            vp.addVideoComm(vc);

            result.put("success",true);

        }catch (Exception e){
            result.put("success",false);
            result.put("msg","保存出错");
            e.printStackTrace();
        }

        return JSONObject.fromObject(result).toString();

    }

    @RequestMapping("/addVideoLike")
    public String addVideoLike(String param){
        LOGGER.info("addVideoLike:"+param);
        Map result = new HashMap();
        try{
            if(Common.isEmpty(param)){
                result.put("success",false);
                result.put("msg","参数不足");
                return JSONObject.fromObject(result).toString();
            }

            JSONObject json = JSONObject.fromObject(param);
            VideoLike v = new VideoLike();
            String likeName = Common.saveToString(json.getString("likeName"),"");
            int videoId = Common.saveToInteger(json.getString("videoId"),0);
            v.setLikeName(likeName);
            v.setVideoId(videoId);
            vp.addVideoLike(v);
            result.put("success",true);

        }catch (Exception e){
            result.put("success",false);
            result.put("msg","保存出错");
            e.printStackTrace();
        }
        return JSONObject.fromObject(result).toString();
    }

    @RequestMapping("/searchVideo")
    public String searchVideo(String param){
        LOGGER.info("searchVideo:"+param);
        Map result = new HashMap();
        try{
            if(Common.isEmpty(param)){
                result.put("success",false);
                return JSONObject.fromObject(result).toString();
            }

            List<Video> videoList = vp.searchVideo(param);
            if(Common.isListNotBlank(videoList)){
                for(int i=0;i<videoList.size();i++){
                    Video v =  videoList.get(i);
                    v.setVideoUrl(URL+v.getVideoUrl());
                    v.setImgUrl(URL+v.getImgUrl());
                }
            }
            result.put("success",true);
            result.put("data", JSONArray.fromObject(videoList).toString());
        }catch (Exception e){
            result.put("success",false);
            e.printStackTrace();
        }
        return JSONObject.fromObject(result).toString();
    }
}
